package com.horsen.service.edu.controller.admin;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.horsen.common.util.R;
import com.horsen.service.edu.controller.BaseController;
import com.horsen.service.edu.model.Chapter;
import com.horsen.service.edu.model.VO.ChapterVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程 控制器
 *
 * @author Horsen
 */
@Api(tags = "章节管理")
@ApiSupport(order = 5)
@RestController
@RequestMapping("/admin/edu/chapters")
public class ChapterController extends BaseController {

    @ApiOperationSupport(order = 1)
    @ApiOperation("根据ID获取一个章节")
    @GetMapping("/{id}")
    public R queryById(@ApiParam(value = "课程ID", required = true)
                           @PathVariable String id) {
        Chapter chapter = chapterService.getById(id);
        return chapter != null ?
                R.ok("获取ID=" + id + "的章节成功").setData("item", chapter) : R.error("获取ID=" + id + "的章节失败");
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("新增一个章节")
    @PostMapping("/")
    public R insert(@ApiParam(value = "章节对象", required = true)
                    @RequestBody Chapter chapter) {
        return chapterService.save(chapter) ?
                R.ok("新增章节成功") : R.error("新增章节失败");
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("修改一个章节")
    @PutMapping("/")
    public R update(@ApiParam("章节对象")
                    @RequestBody Chapter chapter) {
        return chapterService.updateById(chapter) ?
                R.ok("修改章节成功") : R.error("修改章节失败");
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("根据ID删除章节")
    @DeleteMapping("/{id}")
    public R deleteById(@ApiParam(value = "章节ID", required = true)
                        @PathVariable String id) {
        return chapterService.deleteById(id) ?
                R.ok("删除ID=" + id + "的章节成功") : R.error("删除ID=" + id + "的章节失败");
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation("获取章节>视频树形列表")
    @GetMapping("/tree/{courseId}")
    public R getTreeList(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String courseId){
        List<ChapterVO> treeList = chapterService.getTreeList(courseId);
        return R.ok().setData("items", treeList);
    }
}

